<template>
  <div class="scanner">
    <van-nav-bar title="B2C分货打印" left-text="返回" @click-left="onClickLeft"></van-nav-bar>
    <van-row style="margin-top: 0.5rem;" type="flex" justify="center">
      <van-col>
        <van-form :model="formItem" ref="formItem">
          <van-field
            v-model="formItem.printerName"
            id="printerName"
            ref="printerName"
            name="打印机名称"
            label="打印机名称"
            placeholder="打印机名称"
            @keyup.enter.native="searchPrintNameData"
            @focus="printerNameFocusClick"
            @click="printerNameFocusClick"
            :readonly="printerNameReadOnly"
            :rules="[{required: true, message: '请扫描打印机名称'}]"
          />
          <van-field v-model="formItem.shopPrintName" id="shopPrintName" ref="shopPrintName" label="快递面单打印机名称" :disabled="true" />
          <van-field
            v-model="formItem.expressPrintName"
            id="expressPrintName"
            ref="expressPrintName"
            label="购物清单打印机名称"
            :disabled="true"
          />
        </van-form>
      </van-col>
    </van-row>
    <van-overlay :show="showMsgOverlay" @click="showMsgOverlay = false">
      <div class="wrapper">
        <div class="msgTxt">{{ showMsg }}</div>
      </div>
    </van-overlay>
  </div>
</template>
<script>
import {mapGetters} from "vuex";
import {printPross} from "@/api/sortScan/sortScan";
import {printShoppingListWithBatch} from "@/api/sortScan/printShopping";
import {expressBatchPrint, getPrinterName} from "@/api/sortScan/printExpress";
import {getPrintConfig} from "@/api/express/projectPrintConfig";
import {getPrinterTempInfo} from "@/api/sortScan/printTempConfig";
export default {
  name: "b2cSortScan",
  computed: {
    ...mapGetters("scanner", ["pdaScanData"]),
    ...mapGetters("user", ["userData"])
  },
  watch: {
    $route: {
      handler(route) {
        if (route.name === "sortScanPrint") {
          this.formItem.pickTaskCode = this.$route.query.pickTaskCode;
          this.formItem.storerCode = this.$root.storerWarehouseBrand.storerCode;
          this.printNameFocus();
          this.formItem.printerName = "";
          this.formItem.expressPrintName = "";
          this.formItem.shopPrintName = "";
        }
      }
    },
    deep: true,
    pdaScanData: {
      handler(data) {
        let input = document.activeElement.id;
        if (input == "printerName") {
          this.formItem.printerName = data.data;
          this.searchPrintNameData();
        }
      },
      deep: true
    }
  },
  mounted() {
    this.printNameFocus();
    this.formItem.pickTaskCode = this.$route.query.pickTaskCode;
    this.formItem.storerCode = this.$root.storerWarehouseBrand.storerCode;
  },
  data() {
    return {
      formItem: {
        printerName: "",
        expressPrintName: "",
        shopPrintName: "",
        pickTaskCode: "",
        storerCode: ""
      },
      printBtnDisabled: false,
      printerNameReadOnly: false,
      showMsgOverlay: false,
      showMsg: ""
    };
  },
  methods: {
    searchPrintNameData() {
      if (this.formItem.printerName == "") {
        this.$toast.fail("快递面单打印机名称不能为空");
        this.printNameFocus();
      } else {
        getPrinterTempInfo(this.formItem.printerName).then(response => {
          if (response.data.status) {
            let pageData = response.data.data;
            this.formItem.expressPrintName = pageData.expressName;
            this.formItem.shopPrintName = pageData.shoppingName;
            this.printTo();
          } else {
            this.formItem.expressPrintName = "";
            this.formItem.shopPrintName = "";
            this.msgShowTime(response.data.message);
          }
        });
      }
    },
    printNameFocus() {
      this.$nextTick(() => {
        this.$refs["printerName"].focus();
      });
    },
    printerNameFocusClick() {
      this.printerNameReadOnly = true;
      setTimeout(() => {
        this.printerNameReadOnly = false;
      }, 200);
    },
    onClickLeft() {
      this.$router.push({
        name: "sortScan"
      });
    },
    printTo() {
      if (this.formItem.expressPrintName == "") {
        this.$toast.fail("面单打印机名称不能为空");
        this.printNameFocus();
      } else if (this.formItem.shopPrintName == "") {
        this.$toast.fail("清单打印机名称不能为空");
        this.printNameFocus();
      } else {
        this.printBtnDisabled = true;
        getPrintConfig(this.formItem.storerCode, "1")
          .then(response => {
            let printConfig = response.data;
            if (printConfig.status) {
              let shopprintTemplate = printConfig.data[0].templateName;
              getPrinterName("uq_ZTO_Delivery", shopprintTemplate, this.userData.sysUserLoginName)
                .then(response => {
                  if (response.data.status) {
                    let expressPageData = response.data.data.expressPrinterName;
                    let shoppingPageData = response.data.data.shoppingPrinterName;
                    let expressClientId = "";
                    let shoppingClientId = "";
                    for (let i = 0; i < expressPageData.length; i++) {
                      if (expressPageData[i].printerName == this.formItem.expressPrintName) {
                        expressClientId = expressPageData[i].clientId;
                        break;
                      }
                    }
                    for (let i = 0; i < shoppingPageData.length; i++) {
                      if (shoppingPageData[i].printerName == this.formItem.shopPrintName) {
                        shoppingClientId = shoppingPageData[i].clientId;
                        break;
                      }
                    }
                    if (expressClientId == "") {
                      this.$toast.fail("快递面单打印机不存在");
                      this.printBtnDisabled = false;
                    } else if (shoppingClientId == "") {
                      this.$toast.fail("购物清单打印机不存在");
                      this.printBtnDisabled = false;
                    } else {
                      printPross(this.formItem.pickTaskCode, this.userData.sysUserLoginName)
                        .then(response => {
                          if (response.data.status) {
                            this.$toast.success(response.data.message);
                            this.printExpressBill(expressClientId);
                            this.printShopping(shoppingClientId, shopprintTemplate);
                            this.$router.push({
                              name: "sortScan"
                            });
                          } else {
                            this.msgShowTime(response.data.message);
                          }
                          this.printBtnDisabled = false;
                        })
                        .catch(() => {
                          this.$toast.fail("请求出错！");
                        });
                    }
                  } else {
                    this.printBtnDisabled = false;
                    this.msgShowTime(response.data.message);
                  }
                })
                .catch(() => {
                  this.$toast.fail("请求出错！");
                });
            } else {
              this.printBtnDisabled = false;
              this.msgShowTime(printConfig.message);
            }
          })
          .catch(() => {
            this.$toast.fail("请求出错！");
          });
      }
    },
    printExpressBill(expressClientId) {
      //打印快递面单
      expressBatchPrint(
        this.formItem.pickTaskCode,
        this.formItem.expressPrintName,
        "uq_ZTO_Delivery",
        expressClientId,
        this.formItem.storerCode
      )
        .then(response => {
          if (response.data.status) {
            this.$toast.fail("快递面单打印成功");
          } else {
            this.$toast.fail("快递面单打印失败，请联系管理员！");
          }
        })
        .catch(() => {
          this.$toast.fail("请求出错!");
        });
    },
    printShopping(shoppingClientId, shopprintTemplate) {
      //打印购物清单
      printShoppingListWithBatch(
        this.formItem.pickTaskCode,
        this.formItem.shopPrintName,
        shoppingClientId,
        this.formItem.storerCode,
        shopprintTemplate
      )
        .then(response => {
          if (response.data.status) {
            this.$toast.fail("购物清单打印成功");
          } else {
            this.$toast.fail("购物清单打印失败，请联系管理员！");
          }
        })
        .catch(() => {
          this.$toast.fail("请求出错!");
        });
    },
    msgShowTime(msg) {
      this.showMsg = msg;
      this.showMsgOverlay = true;
      setTimeout(() => {
        this.showMsgOverlay = false;
      }, 3000);
    }
  }
};
</script>
<style lang="less">
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.msgTxt {
  margin: 8px 83px 0;
  font-size: 18px;
  color: white;
}
</style>
